package com.kgc.ljp.server.view;

import com.kgc.ljp.server.entity.*;
import com.kgc.ljp.server.dao.imp.AdminDaoImp;
import com.kgc.ljp.server.dao.imp.Login;
import com.kgc.ljp.server.dao.imp.UserPlayDaoImp;
import com.kgc.ljp.server.util.Init;
import com.kgc.ljp.server.util.OwnerRegistCode;
import com.kgc.ljp.server.util.SavaCode;

import java.util.*;

/**
 * 菜单类
 */
public class JntmMgr {
    Scanner scan = new Scanner(System.in);
    //全局管理员id
    private String aIds;
    //全局玩家id
    private String uIds;
    //全局变量choose，用于switch的选择
    private String choose = null;
    //用户类父类
    private UserAndAdmin userAdmin;
    //卡片父类
    private PokeCard pc;
    //背包类
    private Bag bag;
    //wujin关卡类
    private WuJingPoint wuJin;
    //剧情关卡类
    private JuQingPoint juQing;
    //用户管理接口实现类
    UserPlayDaoImp userDao = new UserPlayDaoImp();
    //管理员接口实现类
    AdminDaoImp adminDao = new AdminDaoImp();
    //用户
    User user = null;
    /**
     * 一级菜单
     */
    public void firstMenu(){
        boolean flag = false;
        System.out.println("******欢迎使用JNTM&&JiNiTaiMei系统，祝您生活愉快******");
        do {
            System.out.println("1.登录\n2.注册\n3.退出");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    loginMenu();
                    flag = false;
                    break;
                case "2":
                    regeistMenu();
                    flag = false;
                    break;
                case "3":
                    System.out.println("谢谢使用");
                    System.exit(0);
                    break;
                    default:
                        System.out.println("没有此选项，请重新输入!!!");
                        flag = true;
                        break;
            }
        }while (flag);
    }

    /**
     * 登录菜单方法
     */
    public void loginMenu(){
        boolean flag = false;
        do {
            System.out.println("********JNTM&&JiNiTaiMei登录系统*********");
            System.out.println("1.管理员登录\n2.用户登录\n3.返回上一层");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    System.out.println("请输入内部码:");
                    String ownCode = scan.next();
                    if (ownCode.equals(OwnerRegistCode.getCODE())){
                        for (int i = 2; i >= 0; i--) {
                            System.out.println("请输入您的账号:");
                            String aId = scan.next();
                            System.out.println("请输入您的密码:");
                            String aPwd = scan.next();
                            if (Login.adminLogin(aId,aPwd)){
                                System.out.println("登录成功");
                                aIds = aId;
                                adminMenu();
                                break;
                            }else{
                                System.out.println("账号密码不匹配,还有"+i+"次机会");
                            }
                        }
                    }else{
                        System.out.println("您好，你不是我们内部管理员！！！");
                        loginMenu();
                    }
                    flag = false;
                    break;
                case "2":
                    String sCode = null;
                    for (int i = 2; i >= 0; i--) {
                        System.out.println("请输入您的账号:");
                        String uId = scan.next();
                        System.out.println("请输入您的密码:");
                        String uPwd = scan.next();
                        //调用验证码
                        SavaCode.useCode();
                        if (Login.userLogin(uId,uPwd)){
                            System.out.println("登录成功");
                            uIds = uId;
                            user = userDao.print(uId);
                            userMenu();
                            break;
                        }else{
                            System.out.println("账号密码不匹配,还有"+i+"次机会");
                        }
                    }
                    flag = false;
                    break;
                case "3":
                    firstMenu();
                    flag = false;
                    break;
                default:
                    System.out.println("没有此选项！！！");
                    flag = true;
                    break;
            }
        }while(flag);

    }
    /**
     * 注册菜单方法
     */
    public void regeistMenu(){
        boolean flag = false;
        String sCode = null;
        String aId = null;
        String aPwd = null;

        do {
            System.out.println("********JNTM&&JiNiTaiMei注册系统*********");
            System.out.println("1.管理员注册\n2.玩家注册\n3.返回上一层");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    boolean flagA = false;
                    do {
                        System.out.println("请输入内部码:");
                        String ownCode = scan.next();
                        if (ownCode.equals(OwnerRegistCode.getCODE())){
                            System.out.println("请输入您的账号:");
                            aId = scan.next();
                            do {
                                System.out.println("请输入您的密码(长度不小于6位):");
                                aPwd = scan.next();
                            }while (!aPwd.matches("\\S{6,}$"));
                            //调用验证码
                            SavaCode.useCode();
                            userAdmin = new Admin(aId,aPwd);
                            if (adminDao.regiest((Admin) userAdmin)){
                                System.out.println("注册成功");
                                flagA = false;
                                firstMenu();
                            }else {
                                System.out.println("注册失败，账号已存在");
                                flagA = true;
                            }
                        }else{
                            System.out.println("您好，注册管理员需要内部管理员验证码！！！");
                            loginMenu();
                        }
                    }while (flagA);
                    flag = false;
                    break;
                case "2":
                    boolean flagU = false;
                    String uId = null;
                    String uPwd =null;
                    String uNikeName = null;
                    String uPhone = null;
                    String uIdCard = null;
                    do {
                        System.out.println("请输入您的账号:");
                        uId = scan.next();
                        do {
                            System.out.println("请输入您的密码(长度不小于6):");
                            uPwd = scan.next();
                            if (uPwd.matches("\\S{6,}$")){
                                break;
                            }else{
                                System.out.println("密码长度不能小于6");
                            }
                        }while (true);
                        System.out.println("请输入您的昵称:");
                        uNikeName = scan.next();
                        do {
                            System.out.println("请输入您的手机号:");
                            uPhone = scan.next();
                            if (uPhone.matches("[1][3|5|7|8|9]\\d{9}$")){
                                break;
                            }else{
                                System.out.println("请输入正确的手机号");
                            }
                        }while (true);
                        do {
                            System.out.println("请输入您的身份证号:");
                            uIdCard = scan.next();
                            if (uIdCard.matches("\\d{15}$")){
                                break;
                            }else if (uIdCard.matches("\\d{18}$")){
                                break;
                            }else {
                                System.out.println("请输入正确的身份证号");
                            }
                        }while (true);
                        //调用验证码
                        SavaCode.useCode();
                        userAdmin = new User(uId,uPwd,10,uNikeName,uPhone,uIdCard,1, Init.heroClassMap.get("C级叫花鸡"),null);
                        if (userDao.regiest((User) userAdmin)){
                            System.out.println("注册成功,系统赠送您一只可爱的"+Init.heroClassMap.get("C级叫花鸡").getName());
                            flagU = false;
                            firstMenu();
                        }else{
                            System.out.println("注册失败，账号已存在");
                            flagU = true;
                        }
                    }while (flagU);
                    flag = false;
                    break;
                case "3":
                    firstMenu();
                    flag = false;
                    break;
                default:
                    System.out.println("没有此选项！！！");
                    flag = true;
                    break;
            }
        }while(flag);
    }

    /**
     * 管理员菜单
     */
    public void adminMenu(){
        boolean flag = false;
        System.out.println("******欢迎管理员"+aIds+"，祝您生活愉快******");
        do {
            System.out.println("1.卡片管理\n2.关卡管理\n3.用户管理\n4.返回上一层");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    adminCardManageMenu();
                    flag = false;
                    break;
                case "2":
                    adminCileManageMenu();
                    flag = false;
                    break;
                case "3":
                    adminUsersManageMenu();
                    flag = false;
                    break;
                case "4":
                    firstMenu();
                    flag = false;
                    break;
                default:
                    System.out.println("没有此选项，请重新输入!!!");
                    flag = true;
                    break;
            }
        }while (flag);
    }

    /**
     * 管理员卡片管理菜单
     */
    public void adminCardManageMenu(){
        boolean flag = false;
        System.out.println("******卡片管理界面******");
        do {
            System.out.println("1.卡片增加\n2.卡片修改\n3.返回上一层");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    System.out.println("请设置卡片名称:");
                    String name = scan.next();
                    System.out.println("请设置卡片攻击:");
                    int power = scan.nextInt();
                    System.out.println("请设置卡片血量:");
                    int blood = scan.nextInt();
                    System.out.println("请设置卡片防御:");
                    int defense = scan.nextInt();
                    System.out.println("请设置卡片技能:");
                    String skill = scan.next();
                    System.out.println("请设置卡片暴击:");
                    int boom = scan.nextInt();
                    if(adminDao.addCard(new HeroClass(name,power,blood,defense,skill,boom))){
                        System.out.println("添加成功");
                        adminMenu();
                    }else {
                        System.out.println("添加失败");
                        adminMenu();
                    }
                    flag = false;
                    break;
                case "2":
                    System.out.println("敬请期待");
                    adminMenu();
//                    String upCname = scan.next();
                    flag = false;
                    break;
                case "3":
                    adminMenu();
                    flag = false;
                    break;
                default:
                    System.out.println("没有此选项，请重新输入!!!");
                    flag = true;
                    break;
            }
        }while (flag);
    }
    /**
     * 管理员关卡管理菜单
     */
    public void adminCileManageMenu(){
        boolean flag = false;
        System.out.println("******关卡管理界面******");
        do {
            System.out.println("1.关卡增加\n2.关卡修改\n3.返回上一层");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    boolean flag2 = false;
                   do{
                       System.out.println("请选择关卡类型(1.剧情模式  2.无尽模式)：");
                       choose = scan.next();
                       switch (choose){
                           case "1":
                               System.out.println("请设置关卡名称:");
                               String name = scan.next();
                               KunClass kun = Init.kunClassMap.get("虚鲲");
                               if (adminDao.addMap(new JuQingPoint(name,kun))){
                                   System.out.println("添加成功");
                                   adminMenu();
                               }else{
                                   System.out.println("添加失败");
                                   adminMenu();
                               }
                               flag2 = false;
                               break;
                           case "2":
                               System.out.println("敬请期待");
                               adminMenu();
                               flag2 = false;
                               break;
                           default:
                               System.out.println("没有此选项");
                               flag2 = true;
                               break;
                       }
                       }while (flag2);
                    flag = false;
                    break;
                case "2":
                    System.out.println("敬请期待");
                    adminMenu();
                    flag = false;
                    break;
                case "3":
                    adminMenu();
                    flag = false;
                    break;
                default:
                    System.out.println("没有此选项，请重新输入!!!");
                    flag = true;
                    break;
            }
        }while (flag);
    }
    /**
     * 管理员玩家管理菜单
     */
    public void adminUsersManageMenu(){
        boolean flag = false;
        System.out.println("******用户管理界面******");
        do {
            System.out.println("1.查看所有玩家用户\n2.返回上一层");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    Map<String,User> userMap = adminDao.selectAll();
                    int i = 1;
                    System.out.println("账号\t\t昵称\t\t电话\t\t身份证号码\t\t\t所佩戴嘤雄");
                    for (User user:userMap.values()) {
                        user.printUser();
                    }
                        System.out.println("请输入违禁账号:");
                        String uId = scan.next();
                        if (adminDao.deleteUser(uId)){
                            System.out.println("封禁成功");
                            adminMenu();
                        }else {
                            System.out.println("请输入正确的账号");
                            adminMenu();
                        }
                    flag = false;
                    break;
                case "2":
                    adminMenu();
                    flag = false;
                    break;
                default:
                    System.out.println("没有此选项，请重新输入!!!");
                    flag = true;
                    break;
            }
        }while (flag);
    }
    /**
     * 用户一级菜单
     */
    public void userMenu(){
        boolean flag = false;
        System.out.println("******欢迎玩家"+uIds+"，祝您生活愉快******");
        do {
            System.out.println("1.开始冒险\n2.补给商店(适度游戏，请勿沉迷)\n3.背包查看\n4.查看个人信息\n5.k币查询与充值\n6.返回上一层");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    System.out.println("****************开始游戏*******************");
                    System.out.println("1.剧情模式\n2.无尽模式\n3.其他任意键返回\n请选择:");
                    choose = scan.next();
                    switch (choose){
                        case "1":
                            playJuQingGamesMenu();
                            break;
                        case "2":
                            playWuJinGamesMenu();
                            break;
                        default:
                            flag = true;
                            break;
                    }
                    break;
                case "2":
                    cardGetMenu();
                    flag = false;
                    break;
                case "3":
                    selectBagMenu();
                    flag = false;
                    break;
                case "4":
                    System.out.println("******信息查看******");
                    user.setBlood(user.getLevel()*10+user.getHero().getBlood());
                    user.setBoom(user.getLevel()+user.getHero().getBoom());
                    user.setDefense(user.getLevel()*2+user.getHero().getDefense());
                    user.setPower(user.getLevel()*2+user.getHero().getPower());
                    System.out.println("账号信息");
                    System.out.println("账号\t\t昵称\t\t电话\t\t身份证号码\t\t\t\t所佩戴嘤雄");
                    user.printUser();
                    System.out.println("游戏信息");
                    System.out.println("攻击\t防御\t血量\t暴击\t等级\t无尽击杀最高领主");
                    user.printPlayer();
                    System.out.println("任意键返回");
                    scan.next();
                    flag = true;
                    break;
                case "5":
                    System.out.println("******充值中心(充钱一时爽)******");
                    String kMoney = null;
                    System.out.println("您的余额为:"+userDao.selectMoney(uIds));
                    do {
                        System.out.print("请输入您要充值的金额，不充值则输入0(限额999)：");
                        kMoney = scan.next();
                        if (kMoney.matches("\\d{0,3}$")){
                            int money = Integer.valueOf(kMoney);
                            userDao.recharge(uIds,money);
                            if (money==0){
                                break;
                            }
                            System.out.println("充值成功，你当前余额为:"+userDao.selectMoney(uIds));
                            break;
                        }else {
                            System.out.println("请输入正确的金额!!");
                        }
                    }while (true);

                    System.out.println("任意键返回");
                    scan.next();
                    flag = true;
                    break;
                case "6":
                    firstMenu();
                    flag = false;
                    break;
                default:
                    System.out.println("没有此选项，请重新输入!!!");
                    flag = true;
                    break;
            }
        }while (flag);
    }
    /**
     * 玩家剧情游戏菜单
     */
    public void playJuQingGamesMenu(){
        Map<String,JuQingPoint> juQing = userDao.selectJuQing();
        Set set = juQing.keySet();
        Iterator it = set.iterator();
        int i = 0;
        String []ciles = new String[juQing.size()];
        System.out.println("*************剧情关卡*************");
        while (it.hasNext()){
            String str = (String) it.next();
            ciles[i] = str;
            System.out.print((++i)+"\t"+str+"\t\t");
            if (i%2==0){
                System.out.println();
            }
        }
        String num = null;
        do {
            System.out.println("\n请选择关卡(其他任意编号返回上一层)：");
            num = scan.next();
            if (num.matches("\\d$")){
                break;
            }else {
                System.out.println("请输入数字!!!");
            }
        }while (true);
        int result = Integer.valueOf(num);
        if (result<=0||result>juQing.size()){
            userMenu();
        }else{
                    System.out.println(ciles[result-1]);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(juQing.get(ciles[result-1]).getScript());
                    if(userDao.playJuQingGame(user.getHero(),juQing.get(ciles[result-1]).getKun(),user)){
                        System.out.println("恭喜您获得了胜利当前等级"+user.getLevel()+"，获得经验10点,当前经验"+user.getEmprical()+",距离升级还剩"+(30-user.getEmprical()));
                    }else{
                        System.out.println("你输了，当前等级"+user.getLevel()+",获得经验5点,当前经验"+user.getEmprical()+",距离升级还剩"+(30-user.getEmprical()));

                    }
                    System.out.println("任意键返回");
                    scan.next();
                    playJuQingGamesMenu();
        }
    }/**
     * 玩家无尽游戏菜单
     */
    public void playWuJinGamesMenu(){
        System.out.println("*************剧情关卡*************");
        List<KunClass> wujin = userDao.selectWuJin().getKuns();
        int floor = userDao.playWuJinGame(user.getHero(),wujin,user);
        user.setKun(wujin.get(floor-2));
        System.out.println("任意键返回上一层");
        scan.next();
        userMenu();
    }
    /**
     * 卡片补给菜单
     */
    public void cardGetMenu(){
        System.out.println("*********补给商店*********");
        System.out.println("1.嘤雄抽取(每次20金币)");
        System.out.println("2.敬请期待");
        System.out.println("按1开始，其他任意键返回上一级");
        String choose = scan.next();
        switch (choose){
            case "1":
                HeroClass hc = userDao.buyCard(uIds);
                if (hc!=null){
                    System.out.println("恭喜您抽到了"+hc.getName());
                    System.out.println("名称\t\t攻击\t血量\t防御\t暴击率\t技能");
                    hc.print();
                }else{
                    System.out.println("余额不足");
                }
                System.out.println("任意键返回上一层");
                scan.next();
                userMenu();
                break;
                default:
                    userMenu();
                    break;
        }
    }
    /**
     * 背包管理菜单
     */
    public void selectBagMenu(){
        Bag bag = userDao.showHero(uIds);
        System.out.println("******精灵查看******");
        System.out.println("编号\t\t名称\t\t攻击\t血量\t防御\t暴击率\t技能");
        int i = 1;
        List<HeroClass> myHero = bag.getHeroes();
        for (HeroClass hc:myHero) {
            System.out.print(i+"\t\t");
            hc.print();
            i++;
        }
        do {
            System.out.println("请选择编号操作，其他数字返回上一层");
            choose = scan.next();
            if (choose.matches("\\d{0,100}$")){
                    break;
            }
        }while (true);
        int num = Integer.valueOf(choose);
        if (num>myHero.size()||num<=0){
            userMenu();
        }else {
            System.out.println("******精灵操作********");
            System.out.println("1.装备嘤雄\n2.删除嘤雄(返还10金币),其他任意键返回上一层");
            System.out.println("请选择:");
            choose = scan.next();
            switch (choose){
                case "1":
                    HeroClass heroC = myHero.get(num-1);
                    user =userDao.updateCard(uIds,heroC);
                    System.out.println("更换成功");
                    user.setBlood(user.getLevel()*10+heroC.getBlood());
                    user.setBoom(user.getLevel()+heroC.getBoom());
                    user.setDefense(user.getLevel()*2+heroC.getDefense());
                    user.setPower(user.getLevel()*2+heroC.getPower());
                    userMenu();
                    break;
                case "2":
                    if (myHero.size()>1){
                        user = userDao.deleteCard(uIds,num-1);
                        System.out.println("放生成功");
                    }else{
                        System.out.println("最后一只无法放生");
                    }
                    userMenu();
                    break;
                    default:
                        userMenu();
                        break;
            }
        }
    }
    /**
     * mian方法入口
     * @param args
     */
    public static void main(String[] args) {
        JntmMgr sm = new JntmMgr();
        sm.firstMenu();
    }
}
